﻿using MyX.Core;
using MyX.Message;
using MyX.Work;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyX.Bus
{
    public class XEventBus : XMessageBus<XEvent>
    {
        public XEventBus(XIMQ mq) : base("Event", mq)
        {
        }

        protected override void ReceiveCore(XEvent message)
        {
            var status = new XWorkStatus(message.ID);
            foreach (var work in WorkTables.EventWorks)
            {
                if (work.Filter(message))
                {
                    try
                    {
                        status.IsHandle = true;
                        work.Do(message);
                    }
                    catch (Exception e)
                    {
                        status.IsSuccess = false;
                        status.Result = e.Message;
                        status.Exception = e;
                    }
                }
            }
            XClient.StausBus.Send(status);
        }

        protected override void SendCore(XEvent message)
        {
            message.Check();
        }

    }
}
